package com.weetop.aklyscan.activity;

import android.app.Dialog;
import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.classic.adapter.BaseAdapterHelper;
import com.classic.adapter.CommonRecyclerAdapter;
import com.weetop.aklyscan.R;
import com.weetop.aklyscan.appConfig.MyApplication;
import com.weetop.aklyscan.bean.MailBean;
import com.weetop.aklyscan.bean.MailBean1;
import com.weetop.aklyscan.presenter.SendOutPresent;
import com.weetop.aklyscan.utils.BaseUtils;

import java.util.ArrayList;
import java.util.List;

public class MailDialog extends Dialog {
    private Context mContext;
    private ImageView back;
    private RecyclerView recyclerView;
    private TextView mailName, mailCode;

    private CommonRecyclerAdapter<MailBean.DataBean> adapter;
    private CommonRecyclerAdapter<MailBean1.DataBean> adapter1;
    private List<MailBean.DataBean> mailList = new ArrayList<>();
    private List<MailBean1.DataBean> mailList1 = new ArrayList<>();

    private SendOutPresent sendOutPresent;

    public MailDialog(Context context, List<MailBean.DataBean> mailList, TextView mailName, TextView mailCode) {
        super(context, R.style.promptDialog);
        setContentView(R.layout.dialog_mail);
        mContext = context;
        this.mailList = mailList;
        this.mailName = mailName;
        this.mailCode = mailCode;
        initVar();
        initView();
        initAttr2();
        initAction();
    }

    public MailDialog(Context context, List<MailBean1.DataBean> mailList1, TextView mailName, TextView mailCode, SendOutPresent sendOutPresent) {
        super(context, R.style.promptDialog);
        setContentView(R.layout.dialog_mail);
        mContext = context;
        this.mailList1 = mailList1;
        this.mailName = mailName;
        this.mailCode = mailCode;
        this.sendOutPresent = sendOutPresent;
        initVar();
        initView();
        initAttr1();
        initAction();
    }

    public void initVar() {
    }

    public void initView() {
        back = findViewById(R.id.back);
        recyclerView = findViewById(R.id.recyclerView);
    }

    public void initAttr1() {
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        adapter1 = new CommonRecyclerAdapter<MailBean1.DataBean>(mContext, R.layout.item_mail_type, mailList1) {
            @Override
            public void onUpdate(BaseAdapterHelper helper, MailBean1.DataBean item, int position) {
                helper.setText(R.id.mail_name, item.getSon_name());
                helper.setText(R.id.mail_id, item.getId());
                String code = MyApplication.getMailID();
                if (code.equals(item.getId())) {
                    helper.setBackgroundRes(R.id.moren, R.drawable.et_grey);
                    helper.setTextColorRes(R.id.moren, R.color.color333);
                } else {
                    helper.setBackgroundRes(R.id.moren, R.drawable.tv_green);
                    helper.setTextColorRes(R.id.moren, R.color.white);
                }
                helper.setOnClickListener(R.id.button, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (BaseUtils.fastClick()) {
                            if (mailList1.size() > position) {
                                mailName.setText(mailList1.get(position).getSon_name());
                                Fragment_send_out.mailCode1 = mailList1.get(position).getCode();
                                Fragment_send_out.mailID1 = mailList1.get(position).getId();
                                Fragment_send_out.templateCode = mailList1.get(position).getTemplate_code();
                                mailCode.setText("0");
                                sendOutPresent.surplus(mailList1.get(position).getId());
                            }
                        }
                    }
                });
                helper.setOnClickListener(R.id.moren, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (BaseUtils.fastClick()) {
                            if (mailList1.size() > position) {
                                MyApplication.setMailID(item.getId());
                                notifyDataSetChanged();
                            }
                        }
                    }
                });
            }
        };
        recyclerView.setAdapter(adapter1);
    }

    public void initAttr2() {
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        adapter = new CommonRecyclerAdapter<MailBean.DataBean>(mContext, R.layout.item_mail_type, mailList) {
            @Override
            public void onUpdate(BaseAdapterHelper helper, MailBean.DataBean item, int position) {
                helper.setText(R.id.mail_name, item.getName());
                helper.setText(R.id.mail_id, item.getId());
                String code = MyApplication.getMailCode();
                if (code.equals(item.getId())) {
                    helper.setBackgroundRes(R.id.moren, R.drawable.et_grey);
                    helper.setTextColorRes(R.id.moren, R.color.color333);
                } else {
                    helper.setBackgroundRes(R.id.moren, R.drawable.tv_green);
                    helper.setTextColorRes(R.id.moren, R.color.white);
                }
                helper.setOnClickListener(R.id.button, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (BaseUtils.fastClick()) {
                            if (mailList.size() > position) {
                                mailName.setText(mailList.get(position).getName());
                                mailCode.setText(mailList.get(position).getCode());
                                Fragment_send_out.mailCode1 = mailList.get(position).getCode();
                                Fragment_send_out.mailID1 = mailList.get(position).getId();
                                dismiss();
                            }
                        }
                    }
                });
                helper.setOnClickListener(R.id.moren, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (BaseUtils.fastClick()) {
                            if (mailList.size() > position) {
                                MyApplication.setMailCode(item.getId());
                                notifyDataSetChanged();
                            }
                        }
                    }
                });
            }
        };
        recyclerView.setAdapter(adapter);
    }

    public void initAction() {
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }
}
